﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Wuqi.Webdiyer;
using Lswweb.Domain;

namespace Lswweb.Controls
{
    /// <summary>
    /// 数据显示控件，皮服文件可以指定，数据控件名必须是gridView
    /// </summary>
    public abstract class SkinnedGridView<T> : SkinnedWebControl
    {
        private Control dataBoundControl;
        private AspNetPager aspNetPager= new AspNetPager();
        private int totalRecord;

        public event Func<object, string> BuilderLinkUrlHandler;

        protected abstract IList<T> GridViewDataSourceHandler(int pageIndex, int pageSize, out int totalRecord);

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            GridViewDataBind();
            
            if(EnableAspNetPager)
                this.Controls.Add(aspNetPager);
        }

        protected override void InitializeSkin(System.Web.UI.Control skin)
        {
            dataBoundControl = skin.FindControl("gridView");
            dataBoundControl.EnableViewState = false;

            if (dataBoundControl == null)
                throw new ArgumentNullException("皮肤文件中没有Id为gridView的数据控件。");
        }

        /// <summary>
        /// 注意，这里绝对不能用 override DataBinder()，不然就不能进行嵌套绑定
        /// </summary>
        public void GridViewDataBind()
        {
            //这一句放在aspNetPager配置之上是因为我们需要在这里取得AspNetPager的RecordCount数量，如果未得到这个值而设置AspNetPager的其它属性会出问题。
            Globals.SetPropertyValue(dataBoundControl, "DataSource", GridViewDataSourceHandler(PageIndex, PageSize, out totalRecord));

            aspNetPager.UrlPaging = true;
            aspNetPager.RecordCount = totalRecord;
            aspNetPager.PageSize = PageSize;
            aspNetPager.CurrentPageIndex = PageIndex;
            aspNetPager.HorizontalAlign = HorizontalAlign.Center;

            aspNetPager.LastPageText = this.Strings["AspNetPager_LastPageText"];
            aspNetPager.FirstPageText = this.Strings["AspNetPager_FirstPageText"];
            aspNetPager.NextPageText = this.Strings["AspNetPager_NextPageText"];
            aspNetPager.PrevPageText = this.Strings["AspNetPager_PrevPageText"];
            aspNetPager.CustomInfoHTML = this.Strings["AspNetPager_CustomInfoHTML"];
            aspNetPager.ShowCustomInfoSection = ShowCustomInfoSection.Left;
            aspNetPager.ShowPageIndexBox = ShowPageIndexBox.Always;
            aspNetPager.TextBeforePageIndexBox = this.Strings["AspNetPager_TextBeforePageIndexBox"];

            aspNetPager.SubmitButtonClass = "pageIndexButton";
            aspNetPager.PageIndexBoxClass = "pageIndexBox";
            aspNetPager.CustomInfoClass = "customHtml";
            aspNetPager.CssClass = "aspNetPager";
            aspNetPager.PagingButtonsClass = "pagingButtons";
         

            //注意数据控件的绑定放在这里，如果放在设置aspNetPager之上那么在ascx文件里边调用的aspNetPager控件就是没有经过配置的控件
            dataBoundControl.DataBind();
        }

        public string BuilderLinkUrl(object obj)
        {
            if (BuilderLinkUrlHandler != null)
                return BuilderLinkUrlHandler(obj);

            return "#";
        }

        public int GetItemIndex(IDataItemContainer container)
        {
            return (this.PageIndex - 1) * this.PageSize + container.DataItemIndex + 1;
        }

        #region properties

        /// <summary>
        /// 索引页，不同于GridView等索引从0开始，当前索引是从1开始。
        /// </summary>
        private int? pageIndex;
        public int PageIndex
        {
            get
            {
                //如果没有在页面控件或其它地方设置那么则返回强制设置的面索引
                if (pageIndex != null)
                    return (int)pageIndex;

                object args = Page.Request.QueryString["page"];
                if (args != null)
                    return Convert.ToInt32(args);
                else
                    return 1;
            }
            set { pageIndex = value; }
        }

        private bool enableAspNetPager=true;
        public bool EnableAspNetPager
        {
            get { return enableAspNetPager; }
            set
            {
                ///不启用分页控件时，配置读取数据的当前页索引和页记录数量
                if (!value)
                {
                    //如果没有在页面控件或其它地方设置，将设置为默认页索引1
                    if (this.pageIndex == null)
                        pageIndex = 1;

                    //如果没有在页面控件或其它地方设置页记录数量，则设置为最大值999
                    if (this.pageSize == null)
                        pageSize = 999;
                }

                enableAspNetPager = value;
            }
        }

        private int? pageSize;
        /// <summary>
        /// 每页显示记录条数
        /// </summary>
        public int PageSize
        {
            get
            {
                if (pageSize == null)
                    return 30;

                return (int)pageSize;
            }
            set { pageSize = value; }
        }

        public int RecordCount
        {
            get
            {
                this.EnsureChildControls();
                return this.aspNetPager.RecordCount;
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty)] 
        protected AspNetPager AspNetPagerControl
        {
            get { return aspNetPager; }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerProperty)] 
        protected Control GridViewControl
        {
            get{return dataBoundControl; }
        }

        /// <summary>
        /// 标题文字
        /// </summary>
        public string HeadText { get; set; }

        /// <summary>
        /// 一个汉字占两个字符
        /// </summary>
        private int _cutLength = int.MaxValue;
        public int CutLength 
        {
            get { return _cutLength ;}
            set { _cutLength=value ;}
        }

        #endregion
    }
}
